﻿using Castle.DynamicProxy;
using System.Reflection;

namespace Luka;

/// <summary>
/// Aop扩展
/// </summary>
[NotScanned]
internal static class AopExtension
{
    /// <summary>
    /// 判断是否是异步方法
    /// </summary>
    /// <returns></returns>
    public static bool IsAsyncMethod(this MethodInfo method)
    {
        return method.ReturnType.Equals(typeof(Task)) || (method.ReturnType.IsGenericType && method.ReturnType.GetGenericTypeDefinition().Equals(typeof(Task<>)));
    }

    /// <summary>
    /// 异步执行
    /// </summary>
    /// <returns></returns>
    public static async Task ProceedAsync(this IInvocation invocation)
    {
        // 异步方法中调用 Proceed()
        invocation.Proceed();
        // 等待异步方法完成
        await (Task)invocation.ReturnValue;
    }
}